package com.odt.outdoor.controller;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import com.odt.outdoor.R;
import com.odt.outdoor.core.BaseActivity;
import com.odt.outdoor.fragments.FindFragment;
import com.odt.outdoor.fragments.MineFragment;
import com.odt.outdoor.fragments.MoveFragment;
import com.odt.outdoor.fragments.TrajectoryFragment;
import com.odt.outdoor.widget.tab.ITabFragment;
import com.odt.outdoor.widget.tab.TabLayout;
import com.odt.outdoor.widget.tab.TabView;
import com.shaomengjie.okhttp.AppException;
import java.util.ArrayList;

public class MainActivity extends BaseActivity implements TabLayout.OnTabClickListener {
    private TabLayout mTabLayout;
    private ArrayList<TabView.Tab> tabs;
    private int currentIndex;
    private ITabFragment mCurrentFragment;
    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void initView() {
        mTabLayout = (TabLayout) findViewById(R.id.mTabLayout);
        translucentStatus();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        tabs = new ArrayList<>();
        tabs.add(new TabView.Tab(R.drawable.selector_tab_tragrval_btn, R.string.tab_tratle, R.drawable.selector_tab_label, R.menu.menu_collect_money, TrajectoryFragment.class));
        tabs.add(new TabView.Tab(R.drawable.selector_tab_move_btn, R.string.tab_move, R.drawable.selector_tab_label, MoveFragment.class));
        tabs.add(new TabView.Tab(R.drawable.selector_tab_find_btn, R.string.tab_find, R.drawable.selector_tab_label, FindFragment.class));
        tabs.add(new TabView.Tab(R.drawable.selector_tab_mine_btn, R.string.tab_mine, R.drawable.selector_tab_label,MineFragment.class));
        mTabLayout.initData(tabs, this);
        mTabLayout.setCurrentTab(0);
    }

    @Override
    protected void initLinstener() {

    }

    @Override
    public boolean onGlobalExceptionHandler(AppException e) {
        return false;
    }

    @Override
    public void onTabItemClick(int index, TabView.Tab tab) {
        try {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment to = fm.findFragmentByTag(index + "");
            Fragment from = fm.findFragmentByTag(currentIndex + "");
            switch (index) {
                case 0:

                    break;
                case 1:
                    break;
                case 2:
                    break;
                case 3:
                    break;
            }
            if (to == null) {
                to = (Fragment) tab.mFragmentClazz.newInstance();
                if (from == null) {
                    ft.add(R.id.mContainer, to, index + "").commitAllowingStateLoss();
                } else {
                    ft.hide(from).add(R.id.mContainer, to, index + "").commitAllowingStateLoss();
                }
            } else {
                ft.hide(from).show(to).commitAllowingStateLoss();
            }
            mCurrentFragment = (ITabFragment) to;
            currentIndex = index;
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
}
